<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>发票签收</title>
<base href="<%=basePath%>">
<script type="text/javascript"
	src="plugins/websocketInstantMsg/ext4/ext-all-debug.js"></script>
<script type="text/javascript" src="static/js/jquery-1.7.2.js"></script>
<script type="text/javascript"
	src="plugins/websocketInstantMsg/ext4/locale/ext-lang-zh_CN.js"></script>
<link rel="stylesheet"
	href="plugins/websocketInstantMsg/ext4/resources/css/ext-all.css" />
</head>

<body>
<input id="busDept" name="busDept" type="hidden" value="${bus_dept}"/>
<div id="div1" style="height:70px"></div>
<div id="aa"></div>
</body>
<script type="text/javascript">
Ext.onReady(function () {
	Ext.QuickTips.init();
    var store = Ext.create('Ext.data.Store', {
        fields:  ["id","invoice_code","invoice_title","invoice_item","amount","currency","exchange_rate","bus_dept",
                  "bus_dept_signer","sign_date","ticket_holder","ticket_date","business_sign","business_signer","business_sign_date",
                  "bus_sign_status","fin_sign","fin_signer","fin_sign_date","ticket_entry_mode"],
        pageSize: 15,  //页容量5条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        remoteFilter: true,
        proxy: {
            type: 'ajax',
            url: '<%=basePath%>receipt/list',
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'result.list',  //数据
                totalProperty: 'result.total' //数据总条数
            }
        },
        sorters: [{
			property: 'ordeId',
			direction: 'desc'
		}],
        autoLoad: true  //即时加载数据
    });
    //币种下拉
	var bizhong = Ext.create('Ext.data.Store', {
		fields: ['id', 'name'],
		data : [
            {"id":"", "name":"选择币种"},
			{"id":"RMB", "name":"RMB"},
			{"id":"USD", "name":"USD"},
			
		]
	});
	 //币种下拉
	var bizhong1 = Ext.create('Ext.data.Store', {
		fields: ['id', 'name'],
		data : [
            {"id":"", "name":"选择币种"},
			{"id":"RMB", "name":"RMB"},
			{"id":"USD", "name":"USD"},
			
		]
	});
   
//新建的列表
    var grid = Ext.create('Ext.grid.Panel', {
    renderTo: "aa",
    id:'grid',
    store: store,
    height: 400,
    width:"100%",
    
    //selModel:Ext.create('Ext.selection.CheckboxModel',{mode:"SIMPLE"}),
    selModel: { selType: 'checkboxmodel' },   //选择框
    columns: [                    
                  { text: 'id', dataIndex: 'id',  maxWidth:50 ,hidden:true,},
                  { text: '发票号', dataIndex: 'invoice_code', align: 'left', minWidth: 80, locked: true, },
                  { text: '发票抬头', dataIndex: 'invoice_title',  maxWidth: 140,locked: true, },   
                  { text: '发票名目', dataIndex: 'invoice_item',  maxWidth: 140, locked: true,},   
                  { text: '金额', dataIndex: 'amount', maxWidth: 120, align: 'left', }, 
                  { text: '币种', dataIndex: 'currency', maxWidth: 120, align: 'left', },
                  { text: '汇率', dataIndex: 'exchange_rate', maxWidth: 120, align: 'left', }, 
                  { text: '业务部门', dataIndex: 'bus_dept', maxWidth: 120, align: 'left', },                  
                  { text: '业务部门签收人', dataIndex: 'bus_dept_signer', maxWidth: 200, align: 'left', }, 
                  { text: '业务签收状态', dataIndex: 'bus_sign_status', maxWidth: 200, align: 'left', }, 
                  { text: '签收日期', dataIndex: 'sign_date', maxWidth: 200, align: 'left', }, 
                  { text: '开票人', dataIndex: 'ticket_holder', maxWidth: 200, align: 'left', }, 
                  { text: '开票日期', dataIndex: 'ticket_date', maxWidth: 200, align: 'left', }, 
                  { text: '商务签收状态', dataIndex: 'business_sign', maxWidth: 200, align: 'left', }, 
                  { text: '商务签收人', dataIndex: 'business_signer', maxWidth: 200, align: 'left', }, 
                  { text: '商务签收时间', dataIndex: 'business_sign_date', maxWidth: 200, align: 'left', }, 
                  { text: '财务签收状态', dataIndex: 'fin_sign', maxWidth: 200, align: 'left', }, 
                  { text: '财务签收人', dataIndex: 'fin_signer', maxWidth: 200, align: 'left', }, 
                  { text: '财务签收日期', dataIndex: 'fin_sign_date', maxWidth: 200, align: 'left', }, 
                  { text: '来票方式', dataIndex: 'ticket_entry_mode', maxWidth: 200, align: 'left', }, 
                 
                 
               ],
    bbar: [{
        xtype: 'pagingtoolbar',
        store: store,
        displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
        emptyMsg: "没有数据",
        beforePageText: "当前页",
        afterPageText: "共{0}页",
        displayInfo: true                 
    }],
  
     tbar:[
     //{text:'查询',iconCls:'a_add',handler :  editAlert},"-",
     {text:'签收',iconCls:'a_edit2',handler: showAlert ,},"-",
     {text:'取消签收',iconCls:'a_edit2',handler: editAlert,},
  ], 
     
});
 //新增   
function showAlert (){
	var a=($("#busDept").val());
	//alert(a)
	if( a.indexOf("商务")>-1){
		var records = grid.getSelectionModel().getSelection();//获取选中一行的值
	 	
	 	 if(records.length==0){
	  			Ext.Msg.alert("提示","请选择需要编辑的记录");return;
	  		}
	 		if(records.length>1){
	  			Ext.Msg.alert("提示","只能编辑一条记录");return;
	  		} 
	 		var id = records[0].data.id;//获取id
	 		//alert(id)
	 		var business_sign = records[0].data.business_sign;//获取商业签收状态
	 		//alert(business_sign)
	 		if(business_sign=="已签收"){
	 			Ext.Msg.alert("提示","商务已签收，不用重复签收");return;
	 		}
	 		Ext.Ajax.request({
					url:  '<%=basePath%>receipt/udite',
				params:{
					business_sign : business_sign,	
					id : id,
				},
				timeout:5000,
				success:function(response){
					 Ext.MessageBox.alert("提示","签收成功");
						var store = Ext.getCmp('grid').getStore();
					 store.reload();
				},
				
			});
	}else if(a.indexOf("财务")>-1){
		var records = grid.getSelectionModel().getSelection();//获取选中一行的值
	 	
	 	 if(records.length==0){
	  			Ext.Msg.alert("提示","请选择需要编辑的记录");return;
	  		}
	 		if(records.length>1){
	  			Ext.Msg.alert("提示","只能编辑一条记录");return;
	  		} 
	 		var id = records[0].data.id;//获取id
		var fin_sign=records[0].data.fin_sign;//获取财务签收状态
	 		//alert(fin_sign)
	 		if(fin_sign=="已签收"){
	 			Ext.Msg.alert("提示","财务已签收，不用重复签收");return;
	 		}
	 		Ext.Ajax.request({
					url:  '<%=basePath%>receipt/updite',
				params:{
					fin_sign : fin_sign,	
					id : id,
				},
				timeout:5000,
				success:function(response){
					 Ext.MessageBox.alert("提示","签收成功");
						var store = Ext.getCmp('grid').getStore();
					 store.reload();
				},
				
			});
		
	}else{
		win.show();	
		
		
	 		
	}
}


//取消签收
function editAlert (){
	
	var records = grid.getSelectionModel().getSelection();//获取选中一行的值
   	 if(records.length==0){
     			Ext.Msg.alert("提示","请选择需要编辑的记录");return;
     		}
     		if(records.length>1){
     			Ext.Msg.alert("提示","只能编辑一条记录");return;
     		}
     		var a=($("#busDept").val());
     		if(a.indexOf("商务")>-1){
     			var id = records[0].data.id;
     			var business_sign = records[0].data.business_sign;//获取商务签收状态
     			var fin_sign=records[0].data.fin_sign;//获取财务签收状态
     			if(business_sign=="未签收"||fin_sign=="已签收"){
     				Ext.Msg.alert("提示","不能取消签收");return;	
     			}
     			Ext.Ajax.request({
					url:  '<%=basePath%>receipt/quxiao',
				params:{
					business_sign : business_sign,	
					id : id,
				},
				timeout:5000,
				success:function(response){
					 Ext.MessageBox.alert("提示","取消签收成功");
						var store = Ext.getCmp('grid').getStore();
					 store.reload();
				},
				
			});
     		}else if(a.indexOf("财务")>-1){
     			var id = records[0].data.id;//获取id
     			var fin_sign=records[0].data.fin_sign;//获取财务签收状态
     			if(fin_sign=="未签收"){
     				Ext.Msg.alert("提示","已经是未签收");return;	
     			}
     			Ext.Ajax.request({
					url:  '<%=basePath%>receipt/caiwu',
				params:{
					fin_sign : fin_sign,	
					id : id,
				},
				timeout:5000,
				success:function(response){
					 Ext.MessageBox.alert("提示","取消签收成功");
						var store = Ext.getCmp('grid').getStore();
					 store.reload();
				},
				
			});
     		} else {
     			var id = records[0].data.id;
         		var bus_sign_status = records[0].data.bus_sign_status;
         		var business_sign = records[0].data.business_sign;//获取商务签收状态
         		if(bus_sign_status=="未签收"||business_sign=="已签收"){
         			Ext.Msg.alert("提示","不能取消签收");return;	
         		}
    			Ext.Ajax.request({
					url:  '<%=basePath%>receipt/yewu',
				params:{
					bus_sign_status : bus_sign_status,	
					id : id,
				},
				timeout:5000,
				success:function(response){
					 Ext.MessageBox.alert("提示","取消签收成功");
						var store = Ext.getCmp('grid').getStore();
					 store.reload();
				},
				
			});
         		
     		}
     		
           
     		
     				
	       

	}
	

	
	//保存按钮
	var save = new Ext.Button({
     	text: '保存',
     	 handler: function(){ 
     		var id = Ext.getCmp('id').getValue();   		
			var invoice_code = Ext.getCmp('invoice_code').getValue(); //发票号
     		//alert(invoice_code)
     		var invoice_title = Ext.getCmp('invoice_title').getValue();//发票抬头
     		//alert(invoice_title)
			var invoice_item = Ext.getCmp('invoice_item').getValue();//发票头目
     		//alert(invoice_item)
     		var amount = Ext.getCmp('amount').getValue();//金额
     		//alert(amount)
     		var currency = Ext.getCmp('currency').getValue();//币种
     		//alert(currency)
     		var exchange_rate = Ext.getCmp('exchange_rate').getValue();//税率
     		//alert(exchange_rate)    		
     		var ticket_holder = Ext.getCmp('ticket_holder').getValue();//开票人
     		//alert(ticket_holder)
     		var ticket_date = Ext.getCmp('ticket_date').getValue();//开票时间
     		//alert(ticket_date)    
     		var ticket_entry_mode = Ext.getCmp('ticket_entry_mode').getValue();//来票方式
     		//alert(ticket_entry_mode);
			var store = Ext.getCmp('grid').getStore();
	  		 Ext.Ajax.request({
     			url: '<%=basePath%>receipt/add',
					params : {
						id : id,
						invoice_code : invoice_code,
						invoice_title : invoice_title,
						invoice_item : invoice_item,
						amount : amount,
						currency : currency,
						exchange_rate : exchange_rate,
						ticket_holder : ticket_holder,
						ticket_date  : ticket_date,
						ticket_entry_mode : ticket_entry_mode,
					},
					method : 'POST',
					success : function() {
						Ext.MessageBox.alert("保存成功", "保存成功");
                         
						win.close();
						store.load();
					},
				});

			}

		});
	//重置按钮
		var reset = new Ext.Button({
			text : '重置',
			listeners : {
				'click' : function() {
					Ext.getCmp('add').form.reset();
				
				}
			}
		}

		);
	//取消按钮
		var cancel = new Ext.Button({
			text : '取消',
			listeners : {
				'click' : function() {
					win.close();
				}
			}
		}

		);

	
		var c1 = Ext.create('Ext.container.Container', {
		    layout: 'column',
		    defaults: { labelAlign: 'left', labelWidth: 65 },
		    items: [
		        {xtype: 'textfield',id:'invoice_code', name: 'invoice_code', columnWidth: .5,padding: '10px', fieldLabel: '发票号',},
		        {xtype: 'textfield',id:'id', name: 'id', columnWidth: .5,padding: '10px', fieldLabel: '发票号',hidden:true},
		        {xtype: 'textfield',id:'invoice_title', name: 'invoice_title',columnWidth: .5,padding: '10px',fieldLabel: '发票抬头'}
		    ]
		});
		var c2 = Ext.create('Ext.container.Container', {
		    layout: 'column',
		    defaults: { labelAlign: 'left', labelWidth: 65 },
		    items: [
		        {xtype: 'textfield', name: 'invoice_item',id:'invoice_item', columnWidth: .5,padding: '10px', fieldLabel: '发票名目'},
		        {xtype: 'textfield', name: 'amount', id:'amount',columnWidth: .5,padding: '10px',fieldLabel: '金额'}
		    ]
		});
		var c3 = Ext.create('Ext.container.Container', {
		    layout: 'column',
		    defaults: { labelAlign: 'left', labelWidth: 65 },
		    items: [
		        {xtype: 'combobox', name: 'currency', id:'currency',columnWidth: .5,padding: '10px', fieldLabel: '币种',store:bizhong,emptyText:'请选择',editable: false,valueField: 'id',
					displayField:'name',},
		        {xtype: 'textfield', name: 'exchange_rate',id:'exchange_rate',columnWidth: .5,padding: '10px',fieldLabel: '汇率'}
		    ]
		});
		
		var c5 = Ext.create('Ext.container.Container', {
		    layout: 'column',
		    defaults: { labelAlign: 'left', labelWidth: 65 },
		    items: [
		        {xtype: 'datefield', name: 'ticket_date',id:'ticket_date', columnWidth: .5,padding: '10px', fieldLabel: '开票时间',editable: false,},
		        {xtype: 'textfield', name: 'ticket_holder',id:'ticket_holder',columnWidth: .5,padding: '10px',fieldLabel: '开票人',}
		    ]
		});
		
		var c7 = Ext.create('Ext.container.Container', {
		    layout: 'column',
		    defaults: { labelAlign: 'left', labelWidth: 65 },
		    items: [
		        {xtype: 'textfield', name: 'ticket_entry_mode',id:'ticket_entry_mode', columnWidth: .5,padding: '10px', fieldLabel: '来票方式'},
		        
		    ]
		});
		
	//新增一个form表单
		var add = Ext.create("Ext.form.Panel", {
             id:'add',
			frame : true,
			width : 456,
			height : 600,

			defaults : {
				anchor : '100%',
				labelWidth : 50,
				labelAlign : "right"
			},
			
			 items: [c1, c2, c3,  c5,c7]
			
			
		});
        //新建一个窗口，把form表单放进去
		var win = Ext.create("Ext.window.Window", {
			title : '新增',
			width : 460,
			height : 400,  
			resizable : true,
			modal : true,
			closable : true,
			maximizable : true,
			minimizable : true,
			closeAction :'hide',//点击第二次
			buttons : [ save, reset, cancel ],
			items : [ add ],
		});
		 //时间引入
		function convertDate(val) {
			return Ext.util.Format.date(new Date(val), 'Y-m-d');
		}      
         //新建查询列表
		var c6 = Ext.create('Ext.container.Container', {
			layout : 'column',
			
			defaults : {
				labelAlign : 'left',
				labelWidth : 60
			},
			items : [
					{
						xtype : 'textfield',
						name : 'invoice_code',
						id : 'a',
						columnWidth : 1 / 4,
						padding : '5px',
						fieldLabel : '发票号',
						emptyText:'请输入发票号',
					},
					{
						xtype : 'textfield',
						name : 'invoice_title',
						id : 'b',
						columnWidth : 1 / 4,
						padding : '5px',
						fieldLabel : '发票抬头',
						emptyText:'请输入发票抬头',
					},{
						xtype : 'textfield',
						name : 'invoice_item',
						id : 'c',
						columnWidth : 1 / 4,
						padding : '5px',
						fieldLabel : '发票名目',
						emptyText:'请输入发票名目',
					},
// 					{
// 						xtype : 'textfield',
// 						name : 'amount',
// 						id : 'd',
// 						columnWidth : 1 / 5,
// 						padding : '5px',
// 						fieldLabel : '金额',
// 						emptyText:'请输入金额',
// 					},
// 					{
// 						xtype : 'textfield',
// 						name : 'exchange_rate',
// 						id : 'f',
// 						columnWidth : 1 / 5,
// 						padding : '5px',
// 						fieldLabel : '汇率',
// 						emptyText:'请输入汇率',
// 					},
					{
						xtype : 'combobox',
						name : 'currency',
						id : 'e',
						columnWidth : 1 / 4,
						padding : '5px',
						fieldLabel : '币种',
						emptyText:'请选择',
						valueField: 'id',
						displayField:'name',
						store : bizhong1,
						editable: false,
					},
// 					{
// 						xtype : 'textfield',
// 						name : 'bus_dept',
// 						id : 'g',
// 						columnWidth : 1 / 4,
// 						padding : '5px',
// 						fieldLabel : '业务部门',
// 						emptyText:'请输入业务部门',
// 					},
					{
						xtype : 'textfield',
						name : 'ticket_holder',
						id : 'h',
						columnWidth : 1 / 4,
						padding : '5px',
						fieldLabel : '开票人',
						emptyText:'请输入开票人',
					},{
						xtype : 'datefield',
						name : 'ticket_date',
						id : 'i',
						columnWidth : 1 / 4,
						padding : '5px',
						fieldLabel : '开票日期',
						emptyText:'请选择',
						editable: false,
					},
					
					{
						xtype : "button",
						padding : '5px',
						text : '查询',
						handler : function() {
					var invoice_code = Ext.getCmp('a').getValue();//发票号
					//alert(invoice_code)				
					var invoice_title = Ext.getCmp('b').getValue();//发票抬头
					//alert(invoice_title)
					var invoice_item = Ext.getCmp('c').getValue();//发票名目
					//alert(invoice_item)
					var currency = Ext.getCmp('e').getValue();//币种
					//alert(currency)
					//var bus_dept = Ext.getCmp('g').getValue();//业务部门
					//alert(bus_dept)
					var ticket_holder = Ext.getCmp('h').getValue();//开票人
					//alert(ticket_holder)
					var ticket_date = Ext.getCmp('i').getValue();//开票日期
					//alert(ticket_date)
					var store = Ext.getCmp('grid').getStore();
					store.on('beforeload', function(store, options) {
						var new_params = {
								invoice_code : invoice_code, 
								invoice_title : invoice_title, 
								invoice_item : invoice_item,
								currency : currency,
								//bus_dept : bus_dept,
								ticket_holder : ticket_holder,
								ticket_date : ticket_date,
								
						};
						Ext.apply(store.proxy.extraParams, new_params);
					});
					store.load();
				}
			},{
				xtype : "button",
				padding : '6px',
				text : '清空信息',
				listeners : {
					'click' : function() {
						
						Ext.getCmp('re').form.reset();
					
					}
				}
				
			}
					
					]

		});
           //把查询的列表放进form中
		var form = Ext.create('Ext.form.Panel', {
			renderTo : 'div1',
			frame : true,
			id:'re',
			items : [ c6 ],
// // 		 buttons: [{
// // 		         text: '查询',
// // 		         handler: function() {
// // 		             var form=this.up('form');
// // 		             var id=form.down('textfield[name=id]').value;
// // 		             var uname=form.down('textfield[name=useName]').value;
// // 		             if(!id){
// // 		             alert('编号不能为空！');
// // 		             return;
// // 		             }
// // 		             if(!uname){
// // 		             alert('名称不能为空！');
// // 		             return;
// // 		             }
// // 		            Ext.MessageBox.alert('提示框','提交成功，正在保存数据，请稍后！');
// // 		         }
// // 		   }]
 		});

	});



</script>
</html>